#!/bin/bash
#
# setup jetty servers
#
# 07/25/2012 - created

JETTY_HOSTS="$@"
TMP_DIR=/tmp


function showBar {
 percDone=$(echo 'scale=2;'$1/$2*100 | bc)
 barLen=$(echo ${percDone%'.00'})
 bar=''
 fills=''
 for (( b=0; b<$barLen; b++ ))
 do
  bar=$bar"="
 done
 blankSpaces=$(echo $((100-$barLen)))
 for (( f=0; f<$blankSpaces; f++ ))
 do
  fills=$fills"."
 done
 echo -ne '['$bar'>'$fills']: '$barLen'%\r'
}


# create VMs for hosting the jetty servers
echo
gcutil addinstance --image=projects/google/images/centos-6-2-v20120621 --machine_type=n1-standard-1 --zone=us-central1-a ${JETTY_HOSTS}
echo
echo "Waiting for VMs creation to complete...."
for (( i=0; i<=9600; i++ ))
do
     showBar $i 9600
done
echo











# setup jetty instances
for jt in ${JETTY_HOSTS}
do
     gcutil push ${jt} getjetty.sh ${TMP_DIR} 
     gcutil ssh ${jt} ${TMP_DIR}/getjetty.sh
done

# list URLS for jetty instances
for jt in ${JETTY_HOSTS}
do
     JETTY_URL=`gcutil listinstances | grep ${jt} | cut -d"|" -f7 | tr -d ' '`
     echo
     echo
     echo "jetty url: http://${JETTY_URL}:8080/"
done


exit
